# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=audaspace
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.3.0
pkgrel=2
pkgdesc="A high level audio library written in C++ with language bindings for Python (mingw-w64)"
arch=('any')
license=('Apache')
depends=("${MINGW_PACKAGE_PREFIX}-ffmpeg"
         "${MINGW_PACKAGE_PREFIX}-fftw"
         "${MINGW_PACKAGE_PREFIX}-libsndfile"
         "${MINGW_PACKAGE_PREFIX}-openal"
         "${MINGW_PACKAGE_PREFIX}-python3"
         "${MINGW_PACKAGE_PREFIX}-python3-numpy"
         "${MINGW_PACKAGE_PREFIX}-SDL2")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-doxygen"
             "${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-python3-sphinx"
             "${MINGW_PACKAGE_PREFIX}-pkg-config")
url="https://audaspace.github.io/"
source=(${_realname}-${pkgver}.tar.gz::https://github.com/audaspace/audaspace/archive/v${pkgver}.tar.gz
        0001-Fix-for-seeking-with-modified-pitch.patch
        0002-Bugfix-for-building-with-gcc7.patch
        0003-Make-fftw3-optional.patch
        0004-Build-option-configure-whether-to-build-versioned-pl.patch
        0005-Support-newer-ffmpeg-versions.patch
        0006-Udpate-for-travis.ci.patch
        0007-Some-fixes-backported-from-Blender.patch
        0008-Silence-some-warnings.patch
        0009-Bugfix-memory-leak-in-python-API.patch
        0010-OpenAL-recreate-device-if-disconnected.patch
        0011-Bugfix-don-t-add-non-existing-devices-to-the-device-.patch
        0012-Adding-a-modulator-sound-effect.patch
        0013-Bugfix-Return-correct-length-for-modulator-and-super.patch
        0014-Add-sample-rate-parameter-to-silence-generation.patch
        0015-Minor-CMakeLists.txt-formatting-fixes.patch
        0016-Fix-Missing-include-in-FileManager.h.patch
        0100-mingw-relocate.patch
        0101-mingw-exports.patch)
options=('!strip' 'staticlibs')
sha256sums=('20f6669ca0b9403cdf0b21141a494315d153ad00afccaad72c2e4e86586591d2'
            '1ebc506fa3be5128b5948b91a9457020ab01249f8a39f0dc274164a7c901c0b1'
            '701c872920bf41c2906a75f3a5ff4b8dd8cb4584b545c1c6e4075356831e1578'
            'c9769ed10b49347e247170eaad45a0c5aa3cd9d988ee72a5d3fd3bdf5705092d'
            '8dcd74328ba96c2b7706dfed7bdeffb14aaa420542c84e624ca183e55ff7092e'
            '9006274be9abcb20addff45e5d799f657cf883e66cd37fc189ee2cd2a626723b'
            '22488e28874452d11344b257e6722e73f422a1428d1b5efa1ebf0065a032b541'
            'f829c41015ae55ff2d62907ee810b37a2d19f359c0bf12bc404566920457cc43'
            '677ac49db32619679789833d43bb9460aeef73181be983e038004d402e1a7182'
            '2107ef93d741df438e1d496346e8ef8405b06917c05dfce3edc0e537962893f5'
            '3feef32eda72f56934d26ca2bdd9f30c0601181f2560e6ca4b2ef11776806d28'
            '90d40dd68196fd672191284abd8b294afd0e7f7a71cecbe0dc955ee95d0a0202'
            '63dfea3c6eb8d1d7d630b74f8b8acb7c95521b15964c841ac5780c7c4f523648'
            '890068f35cf1fd36cfd2f90e2b9d0993c9666d06ef283d28b73a872dba04b3cb'
            '05c043204eb616c937716bcad4107eb09578f671bce77fadde7f608fbe84f8bb'
            'fe084eabd8a82bd9a6a92013ff65450356a6d8b076c4dd95b027af6afaeef173'
            '01cac25a1876b75e9ea246554314dca6ae07b13d5b6fcb7365f17a59ebc1b45d'
            'cc68bcc7154c1b06e96e86c587aeafbd6befa5538cf813ee5c42a3ccc13e357d'
            '8cf2aefb1973177ec3d75443e47ba95c66ca113076e615af0a8f6c8de1398da0')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd ${_realname}-${pkgver}
  
  apply_patch_with_msg \
    0001-Fix-for-seeking-with-modified-pitch.patch \
    0002-Bugfix-for-building-with-gcc7.patch \
    0003-Make-fftw3-optional.patch \
    0004-Build-option-configure-whether-to-build-versioned-pl.patch \
    0005-Support-newer-ffmpeg-versions.patch \
    0006-Udpate-for-travis.ci.patch \
    0007-Some-fixes-backported-from-Blender.patch \
    0008-Silence-some-warnings.patch \
    0009-Bugfix-memory-leak-in-python-API.patch \
    0010-OpenAL-recreate-device-if-disconnected.patch \
    0011-Bugfix-don-t-add-non-existing-devices-to-the-device-.patch \
    0012-Adding-a-modulator-sound-effect.patch \
    0013-Bugfix-Return-correct-length-for-modulator-and-super.patch \
    0014-Add-sample-rate-parameter-to-silence-generation.patch \
    0015-Minor-CMakeLists.txt-formatting-fixes.patch \
    0016-Fix-Missing-include-in-FileManager.h.patch \
    0100-mingw-relocate.patch \
    0101-mingw-exports.patch
}

build() {
  declare -a _btype
  if check_option "debug" "y"; then
    _btype=Debug
  else
    _btype=Release
  fi

  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir -p ${srcdir}/build-${MINGW_CHOST} && cd ${srcdir}/build-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=${_btype} \
    ../${_realname}-${pkgver}
  make
}

package() {
  cd "${srcdir}//build-${MINGW_CHOST}"
  make DESTDIR=${pkgdir} -j1 install
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
